package com.sali.a_pre_knowledge.lesson_interface_program.lesson_01;

import com.sali.design_pattern.c_factory_method_pattern.lesson_03.pojo.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @date: 2022/11/13 16:57.
 * @version: 1.0
 */
public class DrawController {

    /**
     * 随机抽取指定数量的用户
     *
     * @param count
     * @return
     */
    public List<User> doRandomUser(List<User> userList, int count) {
        // 如何人数不够，直接返回
        if (userList.size() <= count) {
            return userList;
        }
        // 乱序集合
        Collections.shuffle(userList);
        // 取出指定数量的中奖用户
        List<User> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            list.add(userList.get(i));
        }
        return list;
    }

    /**
     * 按照权重的方式
     *
     * @param count
     * @return
     */
    public List<User> doWeightUser(List<User> userList, int count) {
        // 如何人数不够，之间返回
        if (userList.size() <= count) {
            return userList;
        }
        // 乱序集合
        userList.sort((a, b) -> {
            int e = b.getUserWeight() - a.getUserWeight();
            if (e == 0) {
                return 0;
            }
            return e > 0 ? 1 : -1;
        });

        // 取出指定数量的中奖用户
        List<User> list = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            list.add(userList.get(i));
        }
        return list;
    }

}
